#include <tray.h>
#include <gtk/gtk.h>
#include <string.h>
GtkStatusIcon *tray_icon;
GtkWidget *vinemenu, *vinemenu_config, *vinemenu_about;
const char* default_tray_icon;
void TrayOnClick(GtkStatusIcon *status_icon, gpointer user_data)
{
}
void TrayOnMenu(GtkStatusIcon *status_icon, guint button, guint activate_time, gpointer user_data)
{
	gtk_menu_popup(GTK_MENU(vinemenu), NULL, NULL, 
		       gtk_status_icon_position_menu, 
		       tray_icon, button, activate_time);
}
void MenuAbout(GtkMenuItem* item, gpointer data)
{

}
void MenuConfig(GtkMenuItem* item, gpointer data)
{
}
int SetTrayIcon(const char* filename)
{
	static char cur[256] = {};
	if(strcmp(filename,cur) == 0) return 1;
	strncpy(cur,filename,256);
	gdk_threads_enter();
	gtk_status_icon_set_from_file(tray_icon, cur);
	gdk_threads_leave();
	return 0;
}
int SetTrayToolTip(const char* tips)
{
	gdk_threads_enter();
	gtk_status_icon_set_tooltip_text(tray_icon, tips);
	gdk_threads_leave();
	return 0;
}

int InitTray() 
{
        tray_icon = gtk_status_icon_new();
        g_signal_connect(G_OBJECT(tray_icon), "activate",G_CALLBACK(TrayOnClick), NULL);
        g_signal_connect(G_OBJECT(tray_icon), "popup-menu", G_CALLBACK(TrayOnMenu), NULL);
        gtk_status_icon_set_tooltip(tray_icon, "Vine");
	SetTrayIcon(default_tray_icon);
	vinemenu = gtk_menu_new();
	vinemenu_config = gtk_menu_item_new_with_label("Config");
	vinemenu_about  = gtk_menu_item_new_with_label("About");
	g_signal_connect(G_OBJECT(vinemenu_config), "activate", G_CALLBACK(MenuConfig),NULL);
	g_signal_connect(G_OBJECT(vinemenu_about) , "activate", G_CALLBACK(MenuAbout), NULL);
        gtk_menu_shell_append(GTK_MENU_SHELL(vinemenu), vinemenu_config);
        gtk_menu_shell_append(GTK_MENU_SHELL(vinemenu), vinemenu_about);
	gtk_widget_show_all(vinemenu);
	gtk_status_icon_set_visible(tray_icon, TRUE);
	return 0;
}
